package com.linsir.gen.dto;

import com.google.common.collect.Lists;
import com.linsir.core.util.S;
import com.linsir.gen.entity.SysGenTable;
import com.linsir.gen.entity.SysGenTableColumn;

import java.util.List;

/**
 * @author ：linsir
 * @date ：Created in 2022/10/11 12:56
 * @description：
 * @modified By：
 * @version:
 */
public class SysGenTableColumnDTO extends SysGenTableColumn {

    /*表*/
    private SysGenTable sysGenTable;


    public SysGenTableColumnDTO() {

    }

    public SysGenTableColumnDTO(Long id)
    {
        setId(id);
    }

    public SysGenTableColumnDTO(SysGenTable sysGenTable)
    {
        this.sysGenTable = sysGenTable;
    }




    public String getNameAndComments()
    {
        return getName() + (super.getComments() == null ? "" : new StringBuilder("  :  ").append(super.getComments()).toString());
    }

    public String getDataLength()
    {
        String[] ss = S.split(S.substringBetween(getJdbcType(), "(", ")"), ",");
        if ((ss != null) && (ss.length == 1)) {
            return ss[0];
        }
        return "0";
    }

    public String getSimpleJavaType()
    {
        if ("This".equals(getJavaType())) {
            return S.capitalize(this.sysGenTable.getClassName());
        }
        return S.indexOf(getJavaType(), ".") != -1 ?
                S.substringAfterLast(getJavaType(), ".") :
                getJavaType();
    }

    public String getSimpleJavaField()
    {
        return S.substringBefore(getJavaField(), ".");
    }

    public String getJavaFieldId()
    {
        return S.substringBefore(getJavaField(), "|");
    }

    public String getJavaFieldName()
    {
        String[][] ss = getJavaFieldAttrs();
        return ss.length > 0 ? getSimpleJavaField() + "." + ss[0][0] : "";
    }

    public String[][] getJavaFieldAttrs()
    {
        String[] ss = S.split(S.substringAfter(getJavaField(), "|"), "|");
        String[][] sss = new String[ss.length][2];
        if (ss != null) {
            for (int i = 0; i < ss.length; i++)
            {
                sss[i][0] = ss[i];
                sss[i][1] = S.toUnderScoreCase(ss[i]);
            }
        }
        return sss;
    }

    public List<String> getAnnotationList()
    {
        List<String> list = Lists.newArrayList();
        if ("This".equals(getJavaType())) {
            list.add("com.fasterxml.jackson.annotation.JsonBackReference");
        }
        if ("java.util.Date".equals(getJavaType())) {
            list.add("com.fasterxml.jackson.annotation.JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")");
        }
        if (!isNull() && (!"String".equals(getJavaType()))) {
            list.add("javax.validation.constraints.NotNull(message=\"" + getComments() + "不能为空\")");
        } else if (!isNull() && ("String".equals(getJavaType())) && (!"0".equals(getDataLength()))) {
            list.add("org.hibernate.validator.constraints.Length(min=1, max=" + getDataLength() +
                    ", message=\"" + getComments() + "长度必须介于 1 和 " + getDataLength() + " 之间\")");
        } else if (("String".equals(getJavaType())) && (!"0".equals(getDataLength()))) {
            list.add("org.hibernate.validator.constraints.Length(min=0, max=" + getDataLength() +
                    ", message=\"" + getComments() + "长度必须介于 0 和 " + getDataLength() + " 之间\")");
        }
        return list;
    }

    public List<String> getSimpleAnnotationList()
    {
        List<String> list = Lists.newArrayList();
        for (String ann : getAnnotationList()) {
            list.add(S.substringAfterLast(ann, "."));
        }
        return list;
    }

    public Boolean getIsNotBaseField()
    {
        //todo  需要修改
        if ((!S.equals(getSimpleJavaField(), "id")) &&
                (!S.equals(getSimpleJavaField(), "createBy")) &&
                (!S.equals(getSimpleJavaField(), "createDate")) &&
                (!S.equals(getSimpleJavaField(), "updateBy")) &&
                (!S.equals(getSimpleJavaField(), "updateDate"))
        ) {
            return Boolean.valueOf(true);
        }
        return
                Boolean.valueOf(false);
    }

}
